package org.bulu.project.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.bulu.project.system.mapper.VideoMapper;
import org.bulu.project.system.mapper.VideoStatsMapper;
import org.bulu.project.system.model.dto.VideoSreachDto;
import org.bulu.project.system.model.entity.Video;
import org.bulu.project.system.model.entity.VideoStats;
import org.bulu.project.system.model.vo.VideoVo;
import org.bulu.project.system.security.SecurityUtils;
import org.bulu.project.system.service.VideoSearchService;
import org.bulu.project.system.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.*;


import static java.util.concurrent.TimeUnit.DAYS;

@Service
public class VideoSearchServiceImpl implements VideoSearchService {
    @Autowired
    private VideoMapper videoMapper;
    @Autowired
    private VideoStatsMapper videoStatsMapper;
    @Autowired
    private RedisUtil redisUtil;
    /***
     * 随机获取11条数据
     * @param ids
     * @return
     */
    @Override
    public List<VideoVo> search(Set<Object> ids) {
        List<VideoVo> videoVos = new ArrayList<>();
        for (Object id : ids) {
            VideoVo videoVo = new VideoVo(); // 确保在循环中初始化videoVo

            // 尝试从Redis获取Video对象
            Video video = (Video) redisUtil.getValue("video:" + id);
            if (video == null) {
                // 如果Redis中没有，则从数据库获取
                video = videoMapper.findVideoById((Integer) id);
                // 如果数据库中也没有，则跳过这个ID
                if (video == null) continue;

                // 可选：将获取到的视频对象存回Redis（注意这里应该是设置值而不是运行异步任务）
                redisUtil.setExValue("video:" + id, video, 30, DAYS);
            }

            // 设置VideoVo的属性
            videoVo.setVid(video.getVid());
            videoVo.setUid(video.getUid());
            videoVo.setTitle(video.getTitle());
            videoVo.setCoverUrl(video.getCoverUrl());
            videoVo.setMcId(video.getMcId());
            videoVo.setScId(video.getScId());
            videoVo.setCreateDate(String.valueOf(video.getCreateDate()));

            // 尝试从Redis获取VideoStats对象
            VideoStats videoStats = (VideoStats) redisUtil.getValue("videoStats:" + id);
            if (videoStats == null) {
                // 如果Redis中没有，则从数据库获取
                videoStats = videoStatsMapper.queryById((Integer) id);
                // 如果数据库中也没有，则使用默认值或跳过
                if (videoStats == null) {
                    videoStats = new VideoStats(); // 或者使用默认值初始化
                    // 这里也可以选择不设置任何统计数据
                } else {
                    // 可选：将获取到的统计信息存回Redis
                    redisUtil.setExValue("videoStats:" + id, videoStats, 30, DAYS);
                }
            }

            // 设置VideoVo的统计数据
            videoVo.setPlay(videoStats.getPlay());
            videoVo.setGood(videoStats.getGood());
            videoVo.setComment(videoStats.getComment());

            videoVos.add(videoVo);
        }
        return videoVos;
    }
    /*
    获得所有的视频
 */
    @Override
    public PageInfo findAllVideo(VideoSreachDto videoSreachDto) {
        PageHelper.startPage(videoSreachDto.getPageNum(), videoSreachDto.getPageSize());
        PageInfo pageInfo = new PageInfo<>(videoMapper.findVideo(null));
        return pageInfo;
    }

    /*
        根据分区查询
     */
    @Override
    public PageInfo findVideoByCategory(VideoSreachDto videoSreachDto) {
        PageHelper.startPage(videoSreachDto.getPageNum(), videoSreachDto.getPageSize());
        List<VideoVo> videoVos;
        PageInfo pageInfo;
        if(videoSreachDto.getMcName().equals(videoSreachDto.getScName())){
            videoSreachDto.setScName(null);
            videoVos = videoMapper.findVideo(videoSreachDto);
            if(!videoVos.isEmpty()){
                pageInfo = new PageInfo<>(videoMapper.findVideo(videoSreachDto));
                return pageInfo;
            }
            videoSreachDto.setScName(videoSreachDto.getMcName());
            videoSreachDto.setMcName(null);
            pageInfo = new PageInfo<>(videoMapper.findVideo(videoSreachDto));
            return pageInfo;
        }
        pageInfo = new PageInfo<>(videoMapper.findVideo(videoSreachDto));
        return pageInfo;
    }

    /*
        根据标题查询
     */
    @Override
    public PageInfo findVideoByTitle(VideoSreachDto videoSreachDto) {
        if (videoSreachDto.getTitle() == null) {
            throw new RuntimeException("标题不能为空");
        }
        PageHelper.startPage(videoSreachDto.getPageNum(), videoSreachDto.getPageSize());
        PageInfo pageInfo = new PageInfo<>(videoMapper.findVideo(videoSreachDto));
        return pageInfo;
    }

    /*
        根据视频id查询
     */
    @Override
    public PageInfo findVideoByVideoId(VideoSreachDto videoSreachDto) {
        PageHelper.startPage(videoSreachDto.getPageNum(), videoSreachDto.getPageSize());
        PageInfo pageInfo = new PageInfo<>(videoMapper.findVideo(videoSreachDto));
        return pageInfo;
    }
    /*
        根据用户id查找，当id没有的时候就是用户自己的视频。
     */
    @Override
    public PageInfo findVideoByUserId(VideoSreachDto videoSreachDto) {
        if(videoSreachDto.getUid() == null){
            //获取用户Id
            Long userId = SecurityUtils.getUserId();
            videoSreachDto.setUid(userId);
        }
        PageHelper.startPage(videoSreachDto.getPageNum(), videoSreachDto.getPageSize());
        PageInfo pageInfo = new PageInfo<>(videoMapper.findVideo(videoSreachDto));
        return pageInfo;
    }

    @Override
    public PageInfo findVideoByUserName(VideoSreachDto videoSreachDto) {
        PageHelper.startPage(videoSreachDto.getPageNum(), videoSreachDto.getPageSize());
        PageInfo pageInfo = new PageInfo<>(videoMapper.findVideo(videoSreachDto));
        return pageInfo;
    }

    @Override
    public VideoVo findVideoById(Long vid) {
        VideoSreachDto videoSreachDto = new VideoSreachDto();
        videoSreachDto.setVid(vid);
        return videoMapper.findVideo(videoSreachDto).get(0);
    }

}
